package cn.stylefeng.guns.modular.api.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.UserUtil;
import cn.stylefeng.guns.modular.api.entity.Product;
import cn.stylefeng.guns.modular.api.model.params.ProductParam;
import cn.stylefeng.guns.modular.api.model.result.ProductResult;
import cn.stylefeng.guns.modular.api.service.ProductService;
import cn.stylefeng.guns.modular.api.vo.StatVo;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Api;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 产品控制器
 *
 * @author wusonghe
 * @Date 2019-08-31 00:07:13
 */
@Controller
//@Api(tags="产品管理")
@RequestMapping("/api/product")
public class ProductController extends BaseController {

    private String PREFIX = "/product";

    @Autowired
    private ProductService productService;

    /**
     * 跳转到主页面
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/product.html";
    }

    /**
     * 新增页面
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/product_add.html";
    }

    /**
     * 编辑页面
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/product_edit.html";
    }

    /**
     * 新增接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping("/addItem")
    @ResponseBody
    @ApiOperation(value = "新增接口", notes = "新增", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "新增", response = ResponseData.class)})
    public ResponseData addItem(ProductParam productParam) {
        this.productService.add(productParam);
        return ResponseData.success();
    }

    /**
     * 更新接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping("/editItem")
    @ResponseBody
    @ApiOperation(value = "更新接口", notes = "更新", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "更新", response = ResponseData.class)})
    public ResponseData editItem(ProductParam productParam) {
        this.productService.update(productParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除接口", notes = "删除", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "详情", response = ResponseData.class)})
    public ResponseData delete(ProductParam productParam) {
        this.productService.delete(productParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping("/detail")
    @ResponseBody
    @ApiOperation(value = "详情接口", notes = "详情", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "详情", response = ResponseData.class)})
    public ResponseData detail(ProductParam productParam) {
        Product detail = this.productService.getById(productParam.getProductId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping(value = "/list")
    @ResponseBody
    @ApiOperation(value = "列表接口", notes = "列表", response = ProductResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "列表", response = ProductResult.class)})
    public LayuiPageInfo list(ProductParam productParam, HttpServletRequest request) {
//        User user =  UserUtil.getUserFromToken(request);
//        if (user!=null) {
//            if (user.getFarmId()!=null) {
//                productParam.setFarmId(user.getFarmId().intValue());
//            }
//        }
        return this.productService.findPageBySpec(productParam);
    }

    /**
     * 查询列表
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @PostMapping(value = "/stat")
    @ResponseBody
    @ApiOperation(value = "统计接口", notes = "统计", response = ProductResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "统计", response = ProductResult.class)})
    public StatVo stat() {
        List<Map> mapList = this.productService.stat();
        StatVo statVo = new StatVo();
        List xAxis = new ArrayList();
        List seriesDatas = new ArrayList();
        for (int i = 0; i < mapList.size(); i++) {
            Map map = mapList.get(i);
            xAxis.add(map.get("title"));
            seriesDatas.add(map.get("number"));
        }
        statVo.setXAxis(xAxis);
        statVo.setSeriesDatas(seriesDatas);
        return statVo;
    }
}


